package com.welsy.user.command
{
	import com.adobe.cairngorm.commands.Command;
	import mx.rpc.IResponder;
	import com.adobe.cairngorm.control.CairngormEvent;
	import mx.controls.Alert;
	import com.welsy.usermgt.business.GetSrcCriteriaDelegate;
	import com.welsy.usermgt.model.UserMgtModelLocator;
	import mx.collections.ArrayCollection;
	import com.adobe.cairngorm.model.ModelLocator;
	import com.welsy.util.NamespaceLocator;
	import com.welsy.user.business.GetUserItersDelegate;
	import com.welsy.user.event.GetUserItersEvent;
	import com.welsy.iter.vo.IterVO;
	import com.welsy.user.view.iterview.IterView;
	import com.welsy.user.view.iterview.IterView;
	import com.welsy.user.model.UserModelLocator;
	import com.welsy.user.view.iterview.IterView;
	
	public class GetUserItersCommand implements Command, IResponder
	{
		
		public function execute(event : CairngormEvent) : void {
			var delegate : GetUserItersDelegate = new GetUserItersDelegate(this);
			var eve : GetUserItersEvent = GetUserItersEvent(event);
			delegate.getUserIters(eve.userId, eve.roleId);
		}
		
		public function result( event : Object ) : void {
			
			var xres : XMLList = XMLList(event.result);
			
			var NS : Namespace = new Namespace(NamespaceLocator.ITER_DATATYPES);
			
			var TAG_ITERSCOLLECTION : QName = new QName(NS, "ItersCollection");
			
			var TAG_ITERID : QName = new QName(NS, "IterId");
			var TAG_TITLE : QName = new QName(NS, "Title");
			var TAG_DESCRIPTION : QName = new QName(NS, "Description");
			var TAG_CREDITS : QName = new QName(NS, "Credits");
			var TAG_WEBLINK : QName = new QName(NS, "WebLink");
			var TAG_NOTE : QName = new QName(NS, "Note");
			var TAG_STATE : QName = new QName(NS, "State");
			var TAG_AUTHOR : QName = new QName(NS, "AuthorId");
			var TAG_PARENTITERID : QName = new QName(NS, "ParentIterId");
			var TAG_CREDITSNUMBER : QName = new QName(NS, "CreditsNumber");
			
			var itinera : ArrayCollection = new ArrayCollection;
			var coll : XMLList = xres.descendants(TAG_ITERSCOLLECTION).children();
			for each(var eiter : XML in coll) {
				var buffer : IterVO = new IterVO;
				buffer.id = eiter.child(TAG_ITERID);
				buffer.title = eiter.child(TAG_TITLE);
				buffer.description = eiter.child(TAG_DESCRIPTION);
				buffer.credits = eiter.child(TAG_CREDITS);
				buffer.webLink = eiter.child(TAG_WEBLINK);
				buffer.notes = eiter.child(TAG_NOTE);
				buffer.state = eiter.child(TAG_STATE);
				buffer.authorId = eiter.child(TAG_AUTHOR);
				buffer.parentIterId = eiter.child(TAG_PARENTITERID);
				buffer.creditsNumber = eiter.child(TAG_CREDITSNUMBER);
				var gb : IterView = new IterView;
				gb.iter = buffer;
				itinera.addItem(gb);
			}
			UserModelLocator.getInstance().itervws = itinera;
		}
		
		public function fault(event : Object) : void {
			Alert.show("GetUserItersCommand.fault");
		}
		
	}
}